// miniprogram/pages/regist/regist.js

const app = getApp();

var image_path = "";
var image_base64;

import req from "../../utils/request";
import {
  toast
} from "../../utils/toast";

import {
  USER_REGIST,
  WeChartLogind,
  SCHOOL_GET_LIST,
} from "../../messages/uris";

Page({
  mixins: [require('../../mixin/themeChanged')],

  /**
   * 页面的初始数据
   */
  data: {
    //用户条例
    userRuleChecked: false,
    //控制按钮是否可以被点击
    btnDisabled: true,
    btnNameDisabled: true,
    btnPhoneDisabled: false,
    btnSchoolDisabled: false,
    result: [],
    schoolName: "",
    campus_index: 0,
    user_info: {},
    schoolList: [],
    //弹出层
    topTips: false,
    hide: false,
    tipValue: ''
  },

  open: function() {
    this.setData({
        topTips: true
    });
    setTimeout(() => {
        this.setData({
            topTips: false,
            hide: false,
        });
    }, 4000);
},

  bindAgreeChange(e) {
    //同意勾选
    if (e.detail.value.length) {
      this.userRuleChecked = true;
    } else {
      this.userRuleChecked = false;
    }
  },
  //昵称输入框移除事件
  btnEvenNamethandle(e) {
    console.log(" ---" + e.detail.value);
    if (e.detail.value == '') {
      console.log(" ---" + e.detail.value);
      this.btnNameDisabled = false;
      this.checkFormData();
      return;
    }
    this.btnNameDisabled = true;
    this.checkFormData();

  },
  btnEvenPhonethandle(e) {
    if (e.detail.value == '') {
      this.btnPhoneDisabled = false;
      this.checkFormData();
      return;
    }
    this.btnPhoneDisabled = true;
    this.checkFormData();
  },
  //学校输入框移除事件
  btnEvenSchoolthandle: function (e) {
    console.log("++++" + e.detail.value);
    if (e.detail.value == '') {
      console.log(" ---" + e.detail.value);
      this.btnSchoolDisabled = false;
      this.checkFormData();
      return;
    }
    this.btnSchoolDisabled = true;
    this.checkFormData();
  },
  //校验参数是否合法 按钮是否可以点击
  checkFormData: function () {
    if (this.btnSchoolDisabled && this.btnNameDisabled && this.btnPhoneDisabled) {
      this.setData({
        btnDisabled: false
      });
      return;
    }
    console.log("按钮置灰" + this.btnPhoneDisabled);
    this.setData({
      btnDisabled: true
    });
    return;
  },

  /*--------------------------------------*/
  searchWells: function (e) {
    const that = this;
    var name = e.detail.value;
    this.btnEvenSchoolthandle(e);
    if (name != "") {
      req.get(
        SCHOOL_GET_LIST + "?name=" + name, {},
        (resp) => {
          if (resp.data.result != null) {
            var val = resp.data.result;
            var result = new Array();
            if (name != "" && val != null) {
              //判断用户输入搜素不为空时执行下面的代码
              for (var i = 0; i < val.length; i++) {
                var good = val[i];
                if (good.indexOf(name) > -1) {
                  result.push(good);
                }
              }
              this.setData({
                result: result
              });
            }
          }
        },
        (resp) => {
          console.log("查询失败");
        },(res)=>{},that
      );
    } else {
      this.resetSearch();
    }
  },
  //当用户输入字符后点击恢复默认状态
  resetSearch: function () {
    var result = new Array();
    this.setData({
      result: result,
      schoolName: ""
    });
  },

  //设置选择的学校
  getSchoolName: function (e) {
    var schoolName = e.currentTarget.dataset.index;
    const that = this;
    that.setData({
      schoolName: schoolName,
      result: null,
    });
  },



  //保存的方法
  formSubmit(e) {
    console.log("form发生了submit事件，---携带数据为：", e.detail.value);
    if (!this.userRuleChecked) {
      wx.showModal({
        title: "错误",
        content: "请阅读并勾选用户协议",
        showCancel: false,
      });
      return;
    }
    this.schoolName = e.detail.value.schoolName;
    // 校验参数
    let isOk = this.testFromData(e);
    if (!isOk) {
      return;
      toast("填写信息不完整", "error");
      return;
    }

    const that = this;
    // TODO 未获取用户信息

    wx.login({
      success: (res) => {
        let code = res.code;

        let data = {
          name: e.detail.value.name,
          schoolName: that.schoolName,
          phone: e.detail.value.phone,
          // avatar: this.data.user_info.avatar,
        };

        console.log(data);

        // 请求后端注册
        req.post(USER_REGIST + `?code=${code}`, data, 
        (res) => {
          console.log(res);
          toast("注册成功");
          console.log("注册成功");
          app.globalData.login = true;
          wx.reLaunch({
            url: "../index/index",
          });
         
        }
        ,(res)=>{},(res)=>{},that);
      },
    });
  },

  //重置的方法
  formReset(e) {
    console.log("form发生了reset事件，携带数据为：", e.detail.value);
    this.setData({
      chosen: "",
    });
  },
  /*--------------------------------------*/
  getUserInfo: function () {
    const that = this;
    wx.getUserInfo({
      success: (res) => {
        // this.data.user_info = res.rawData;
        that.setData({
          user_info: res.rawData
        });
        wx.setStorage({
          key: "user_info",
          data: res.rawData,
        });
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },

  /**
   * 改变选择绑定
   * @param {*} e
   */
  bindPickerChange: function (e) {
    var temp = parseInt(e.detail.value);
    this.setData({
      campus_index: temp,
    });
  },
  /**
   * 校验当前页面的表单参数
   */
  testFromData: function (e) {
    console.log(" ++++" + e.detail.value);
    if (this.schoolName == "") {
      wx.showModal({
        title: "错误",
        content: "请选择学校",
        showCancel: false,
      });
      return false;
    }
    return true;
  },
  /**
   * 注册事件
   */
  regist: function (e) {},
});